package core.hmxchen.test.logimpl;/**
 * description: TODO
 *
 * @date: 2021/9/5 18:00
 * @author: hmxchen
 */

import core.hmxchen.log.LogFormatter;
import core.hmxchen.log.entity.MethodInfo;
import core.hmxchen.log.enums.Level;
import core.hmxchen.log.handler.LogHandler;
import org.slf4j.Logger;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 *@ClassName RequestLogImpl
 *@Description TODO
 *@Author hmxchen
 *@version v1.0.0
 *@Date 2021/9/5 18:00 
 */
public class RequestLogImpl implements LogFormatter {
    @Override
    public void format(Logger log, Level level, String busName, MethodInfo methodInfo, Object[] args, String[] filterParamNames, Object result, Throwable throwable) {
        StringBuilder stringBuilder = new StringBuilder();
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        stringBuilder.append("\n【请求路径】:"+request.getRequestURL().toString());
        stringBuilder.append("\n【IP地址】:"+request.getRemoteAddr());
        stringBuilder.append( LogHandler.getAroundInfo(busName,methodInfo,args,filterParamNames,result,throwable));
        LogHandler.print(log,level,stringBuilder.toString());
    }
}
